/**
 * @Title: NestedSetTreeAction.java   
 * @Copyright 2010 -2013 CreativeWise
 * @Package com.cwises.action.tree   
 * @Description:    
 * @author wangxinmin    
 * @date 2014-1-14 下午12:00:34   
 * @version V1.0 
 */
package com.cwises.action.tree;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.cwises.entity.tree.TreeEntity;
import com.cwises.service.tree.TreeService;
import com.opensymphony.xwork2.ActionSupport;
import com.util.GsonUtil;

/**
 * @ClassName: TreeAction
 * @date 2014-1-14 下午12:00:34
 * 
 */
public class TreeAction extends ActionSupport {
	Logger log = Logger.getLogger(this.getClass());
	@Resource(name = "treeService")
	private TreeService treeService;
	private String jsonData;
	private String roleName;
	private String UserId;
	private String ModuleID;
	private String RoleId;
	/**
	 * 
	 * @Title: moduleTree
	 * @Description: 模块树
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @author zhaojie
	 * @date 2014-1-21 下午5:48:22
	 * @throws
	 */
	public String moduleTree() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pid = request.getParameter("pid");
		List<TreeEntity> trList =new ArrayList<TreeEntity>();
		Map<String,Object> mapQuery=new HashMap<String,Object>();
		mapQuery.put("pid", pid);
		if(pid==null||pid.equals("")){
			trList = treeService.findByCondition("findModuleList",mapQuery);// 获取所有节点信息
		}else{
			trList = treeService.findByCondition("findModuleListYB",mapQuery);// 获取所有节点信息
		}
		List<TreeEntity> trListChil=new ArrayList<TreeEntity>();
		for (int i = 0; i < trList.size(); i++) {
			TreeEntity resultTreeEntity = trList.get(i);
			resultTreeEntity.setIsParent(true);
			resultTreeEntity.setNocheck(true);
			trListChil.add(resultTreeEntity);
		}
		JSONArray jarray=new JSONArray();  
		jarray.addAll(trListChil);  
	        HttpServletResponse response = ServletActionContext.getResponse();  
	        response.setCharacterEncoding("utf-8");  
	        PrintWriter pw = null;  
	        try {  
	            pw = response.getWriter();  
	            pw.write(jarray.toString());  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        pw.flush();  
	        pw.close();  
		return SUCCESS;
	}
	
	/**
	/**
	 * 
	 * @Title: RoleTree
	 * @Description: 角色树
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @author zhaojie
	 * @date 2014-1-21 下午1:23:18
	 * @throws
	 */
	public String RoleTree() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		// 转换为ztree需要数据格式 id name pid open checked isParent
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		List<TreeEntity> resultList = new ArrayList<TreeEntity>();
		List<TreeEntity> trList = treeService.findByCondition("findRoleList",null);
		String UserId = request.getParameter("UserId");
		if (UserId != null && !"".equals(UserId)) {
			List<TreeEntity> trListById = treeService.findByCondition("findRoleByUserID", UserId);
			RoleId="";
			for(int j = 0; j < trListById.size(); j++){
				TreeEntity t = null;
				t = trListById.get(j);
				String td = t.getId();
				RoleId += td + ",";
			}
			for (int i = 0; i < trList.size(); i++) {
				TreeEntity resultTreeEntity = null;
				resultTreeEntity = trList.get(i);
				String parentModuleId = resultTreeEntity.getId();
				for (int j = 0; j < trListById.size(); j++) {
					TreeEntity moduleByIdTreeEntity = null;
					moduleByIdTreeEntity = trListById.get(j);
					String childModuleId = moduleByIdTreeEntity.getId();
					if (childModuleId.equals(parentModuleId)
							|| childModuleId == parentModuleId) {
						resultTreeEntity.setChecked(true);
					}
				}
				resultList.add(resultTreeEntity);

			}
		} else {
			resultList = trList;
		}
		for (int i = 0; i < resultList.size(); i++) {
			TreeEntity resultTreeEntity = resultList.get(i);
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("id", resultTreeEntity.getId());
			if ("".equals(resultTreeEntity.getName())
					|| resultTreeEntity.getName() == null) {
				map.put("name", resultTreeEntity.getNameDefult());
			} else {
				map.put("name", resultTreeEntity.getName());
			}
			if (resultTreeEntity.isChecked()) {
				map.put("checked", true);
			}
			map.put("pid", resultTreeEntity.getPid());
			map.put("open", true);

			// 判断是否为父节点
			if (!"0".equals(resultTreeEntity.getPid())) {
				map.put("isParent", false);
			} else {
				map.put("isParent", true);
			}
			// 是否可用
			if (resultTreeEntity.isChkDisabled()) {
				map.put("chkDisabled", true);
			}
			maps.add(map);
		}
		setRoleId(RoleId);
		setUserId(UserId);
		jsonData = GsonUtil.toJson(maps);// 生成json数据
		return SUCCESS;
	}

	/**
	 * 
	 * @Title: rightModuleTree
	 * @Description: 模块数
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @author zhaojie
	 * @date 2014-1-21 下午12:46:08
	 * @throws
	 */
	public String rightModuleTree() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pid = request.getParameter("pid");
		String RoleName = request.getParameter("RoleName");
		List<TreeEntity> trList =new ArrayList<TreeEntity>();
		//查询条件
		Map<String,Object> mapQuery=new HashMap<String,Object>();
		// 转换为ztree需要数据格式 id name pid open checked isParent
		List<TreeEntity> trListChil=new ArrayList<TreeEntity>();
		mapQuery.put("pid", pid);
		if(pid==null||pid.equals("")){
			trList = treeService.findByCondition("findModuleList",mapQuery);// 获取所有节点信息
		}else{
			trList = treeService.findByCondition("findModuleListYB",mapQuery);// 获取所有节点信息
		}
		if (RoleName != null && !"".equals(RoleName)) {
			RoleName = new String(RoleName.getBytes("iso8859-1"),"utf-8"); 
			List<TreeEntity> trListById = treeService.findByCondition("findModuleByModuleID", RoleName);
			ModuleID="";
			for(int j = 0; j < trListById.size(); j++){
				TreeEntity t = null;
				t = trListById.get(j);
				String td = t.getId();
				ModuleID += td + ",";
			}
			// 获取所有节点信息
			for (int i = 0; i < trList.size(); i++) {
				TreeEntity resultTreeEntity = trList.get(i);
				resultTreeEntity.setIsParent(true);
				resultTreeEntity.setTreevalue(ModuleID);
				String parentModuleId = resultTreeEntity.getId();
				for (int j = 0; j < trListById.size(); j++) {
					TreeEntity moduleByIdTreeEntity = null;
					moduleByIdTreeEntity = trListById.get(j);
					String childModuleId = moduleByIdTreeEntity.getId();
					
					if (childModuleId.equals(parentModuleId)
							|| childModuleId == parentModuleId) {
						resultTreeEntity.setChecked(true);
					}
				}
				trListChil.add(resultTreeEntity);
			}
		} else {
			for (int i = 0; i < trList.size(); i++) {
				TreeEntity resultTreeEntity = trList.get(i);
				resultTreeEntity.setIsParent(true);
				resultTreeEntity.setTreevalue(ModuleID);
				trListChil.add(resultTreeEntity);
			}
		}
		JSONArray jarray=new JSONArray();  
		jarray.addAll(trListChil);  
        HttpServletResponse response = ServletActionContext.getResponse();  
        response.setCharacterEncoding("utf-8");  
        PrintWriter pw = null;  
        try {  
            pw = response.getWriter();  
            pw.write(jarray.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        pw.flush();  
        pw.close();  
		setModuleID(ModuleID);
		setRoleName(RoleName);
		
		return SUCCESS;
	}

	/**
	 * @return the jsonData
	 */
	public String getJsonData() {
		return jsonData;
	}

	/**
	 * @param jsonData
	 *            the jsonData to set
	 */
	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}

	/**
	 * @return the roleName
	 */
	public String getRoleName() {
		return roleName;
	}

	/**
	 * @param roleName
	 *            the roleName to set
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	/**
	 * @return the userId
	 */
	public String getUserId() {
		return UserId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(String userId) {
		UserId = userId;
	}

	/**
	 * @return the moduleID
	 */
	public String getModuleID() {
		return ModuleID;
	}

	/**
	 * @param moduleID
	 *            the moduleID to set
	 */
	public void setModuleID(String moduleID) {
		this.ModuleID = moduleID;
	}

	/**
	 * @return the roleId
	 */
	public String getRoleId() {
		return RoleId;
	}

	/**
	 * @param roleId the roleId to set
	 */
	public void setRoleId(String roleId) {
		RoleId = roleId;
	}

}
